<template>
	<view class="content">
		<view class="input-group">
			<image src="../../static/center/logo.png" mode="widthFix" class="top-left"></image>
			<view class="log-lable">
				<image :src="logo" mode="aspectFit" style="height: 250upx;"></image>
			</view>

            <view class="log-input">
                <m-input class="input" type="text" v-model="account" :placeholder="i18n.login.mobile" maxlength="20"></m-input>
            </view>

			<view class="log-input">
				<m-input class="input" type="password" displayable v-model="password" :placeholder="i18n.login.pass" maxlength="20"></m-input>
			</view>

			<view class="memory">
				<label class="radio" @click="checked = !checked">
					<radio value="" v-bind:checked="checked" />{{i18n.login.remember}}
				</label>
				<!-- #ifndef APP -->
						<text @click="jumper('/pages/reg/agree')" class="forget">{{i18n.pages.agree}}</text>
				<!-- #endif -->
			</view>
		</view>

		<view class="btn-row">
			<block v-if='status == 1'>
				<view type="primary" class="primary1" @click="jumper('')">{{i18n.login.login}}</view>
			</block>
			<block v-else>
				<view type="primary" class="primary1">{{i18n.login.login}}</view>
			</block>
		</view>
		
		
		<view class='memory'>
			<text @click="jumper('/pages/login/forget')" class="forget">{{i18n.login.forget}}</text>
			<text @click="jumper('/pages/reg/reg')" class="reg">{{i18n.login.register}}</text>
		</view>
		
		<image :src="back_img" mode="" class="back_img" :style="height"></image>
	</view>
</template>

<script>
	import service from '../../service.js';
	import md5 from '../../store/md5.js';
	import mInput from '../../components/m-input.vue';
	export default {
		computed: {
			i18n () {
				return this.$t('index')
			}
		},
		components: {
			mInput
		},
		data() {
			return {
				back_img:uni.getStorageSync('page'),
				height:uni.getStorageSync('height'),
				providerList: [],
				hasProvider: false,
				account: '',
				password: '',
				status: 1,
				logo: '',
				checked:false
			}
		},
		onShow: function() {
			uni.setNavigationBarTitle({
				title: this.i18n.pages.login
			});
			var page = service.config.pages;
			uni.setStorageSync("page",page);
			uni.setStorageSync('token', '');
			uni.setStorageSync("sec", 1);
			uni.setStorageSync('pop',0);
			this.account = uni.getStorageSync('User_paw')[0]
			if (uni.getStorageSync('memory') == true) {
				this.checked = uni.getStorageSync('memory')
				this.password = uni.getStorageSync('User_paw')[1]
			}
			uni.getSystemInfo({
				success: (res) => {
					var heig = res.windowHeight;
					this.height = 'height:'+heig+'px;';
				}
			});
			service.upload(this, service.api.login.logo, {
				
			}, '', '', function(self, res) {
				if (res.code == 1) {
					self.logo = res.data.logo;
				}
			});
		},
		methods: {
			Login() {
				self = this;
				if (self.account.length < 5) {
					uni.showToast({
						icon: 'none',
						title: self.i18n.login.characters
					});
					return;
				}
				if (self.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: self.i18n.login.minimum
					});
					return;
				}
				self.status = 0;
				service.upload(this, service.api.login.getsalt, {
					'account': self.account
				}, '', '', function(_self, res) {
					var salt = res.data.salt;
					service.upload(this, service.api.login.login, {
						'account': self.account,
						'password': md5(self.password + salt)
					}, '', '', function(_self, res) {
						uni.showToast({
							icon: 'none',
							title: res.info
						});
						if (res.code == 1) {
							uni.setStorageSync('token', res.data.token)
							if (self.checked == true) {
								service.User_paw(self.account, self.password)
								service.Memory(self.checked)
							} else if (self.checked == false) {
								service.Memory(self.checked)
								service.User_paw(self.account, '');
							}
							uni.reLaunch({
								url: '/pages/main/user',
							});
						} else {
							self.status = 1;
							self.password = '';
							uni.setStorageSync('token', '');
						}
					});
				});
			},
			jumper(url) {
				if (url == '') {
					this.Login();
				} else {
					uni.navigateTo({
						url: url
					});
				}
			}
		}
	}
</script>